# 下载图片
import os
import shutil
import sys
import time
import urllib.request

ProjectDir = os.path.dirname(os.path.realpath(sys.argv[0])) + '/云学堂下载数据/'

def main(title,picturelist):
    if not os.path.exists(ProjectDir):
        os.makedirs(ProjectDir)
    workdir = __init(title)
    download(workdir,picturelist)


def __init(title):
    if os.path.exists(ProjectDir + title):
        shutil.rmtree(ProjectDir + title)
    workdir = ProjectDir + title + '/'
    os.mkdir(workdir)
    return workdir

def download(workdir,picturelist):
    picdir = workdir + 'picture/'
    os.mkdir(picdir)
    count = 0
    for mumber in picturelist:
        count = count + 1
        url = mumber
        dest = picdir + str(count) + '.jpeg'
        print('下载中...',url)
        urllib.request.urlretrieve(url,dest)
        if count % 3 == 0:
            time.sleep(1)
    print('共计下载{}张图片'.format(count))




